﻿using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Ha666.DataBase;
using Ha666.WebAPI.Models.Request;

namespace Ha666.WebAPI.Controllers
{
    public class TradeController : ApiController
    {
        private static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static TaoBaoTradeDAL TaoBaoTradeDALProp = new TaoBaoTradeDAL();

        [HttpPost]
        public HttpResponseMessage GetFullInfo(GetFullInfoRequest request)
        {
            HttpResponseMessage response = null;
            try
            {
                string HostName = "";
                var headers = HttpContext.Current.Request.Headers;
                if (headers["HostName"] != null)
                    HostName = headers["HostName"];
                string tokenResult = VerifiedParams.VerfiedToken(request.Token);
                if (tokenResult != "")
                {
                    Logger.Error(string.Format("【GetFullInfo】Token异常，HostName={0},tokenResult={1}", HostName, tokenResult));
                    response = Request.CreateResponse(HttpStatusCode.Forbidden, "");
                    response.ReasonPhrase = HttpUtility.UrlEncode(string.Format("【Trade.GetFullInfo】HostName={0},Error:{1}", HostName, tokenResult));
                    return response;
                }
                if (string.IsNullOrWhiteSpace(request.tid))
                {
                    Logger.Error(string.Format("【GetFullInfo】tid不能为空，HostName={0}", HostName));
                    response = Request.CreateResponse(HttpStatusCode.Forbidden, "");
                    response.ReasonPhrase = HttpUtility.UrlEncode(string.Format("【Trade.GetFullInfo】HostName={0},Error:tid不能为空", HostName));
                    return response;
                }
                long id;
                long.TryParse(request.tid, out id);
                if (id < 10000)
                {
                    Logger.Error(string.Format("【GetFullInfo】tid不能小于10000，HostName={0}", HostName));
                    response = Request.CreateResponse(HttpStatusCode.Forbidden, "");
                    response.ReasonPhrase = HttpUtility.UrlEncode(string.Format("【Trade.GetFullInfo】HostName={0},Error:tid不能小于10000", HostName));
                    return response;
                }
                TaoBaoTrade trade = TaoBaoTradeDALProp.GetItem(id);
                return Request.CreateResponse(HttpStatusCode.OK, trade);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("【GetFullInfo】异常：{0}", exception.Message));
                response = Request.CreateResponse(HttpStatusCode.Forbidden, "");
                response.ReasonPhrase = HttpUtility.UrlEncode("exception:" + exception.Message);
                return response;
            }
        }

        [HttpPost]
        public HttpResponseMessage ClearCache()
        {
            HttpResponseMessage response = null;
            try
            {
                TaoBaoTradeDALProp.ClearItems();
                return Request.CreateResponse(HttpStatusCode.OK, true);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("【GetFullInfo】异常：{0}", exception.Message));
                response = Request.CreateResponse(HttpStatusCode.Forbidden, "");
                response.ReasonPhrase = HttpUtility.UrlEncode("exception:" + exception.Message);
                return response;
            }
        }

    }
}
